﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace laohan.Common.Common
{
    public partial class API
    {
        /// <summary>
        /// 获取时间戳
        /// </summary>
        /// <returns></returns>
        public static long GetTimeStap()
        {
            var javaLongTime = new DateTime(1970, 1, 1, 8, 0, 0);

            var now = DateTime.Now;
            var interval = now - javaLongTime;

            return (long)interval.TotalSeconds;
        }

        /// <summary>
        /// 日期转时间戳
        /// </summary>
        /// <param name="dTime"></param>
        /// <returns></returns>
        public static long GetTimeStap(DateTime dTime)
        {
            var javaLongTime = new DateTime(1970, 1, 1, 8, 0, 0);

            var interval = dTime - javaLongTime;

            return (long)interval.TotalSeconds * 1000;
        }

        /// <summary>
        /// 时间戳转日期
        /// </summary>
        /// <param name="timestamp"></param>
        /// <returns></returns>
        public static DateTime GetDateTime(long timestamp)
        {
            long begtime = timestamp * 10000;

            DateTime dt_1970 = new DateTime(1970, 1, 1, 8, 0, 0);

            long tricks_1970 = dt_1970.Ticks;//1970年1月1日刻度
            long time_tricks = tricks_1970 + begtime;//日志日期刻度

            DateTime dt = new DateTime(time_tricks);//转化为DateTime

            return dt;
        }
    }
}